﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using WinnerCMS.BLL.PageFactory.TagModule;
using WinnerCMS.Common;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL.PageFactory.Pages
{
    /// <summary>
    /// 自定义标签处理类
    /// </summary>
    public class UDT
    {
        /// <summary>
        /// 翻译
        /// </summary>
        /// <param name="TempStrBuilder"></param>
        public static void Translate(StringBuilder TempStrBuilder)
        {
            IList<string> matchresult = TagEngine.TagCollection(TempStrBuilder.ToString(), "WinnerCMS");
            foreach (string nextmatch in matchresult)
            {
                string ID = TagEngine.GetValue(nextmatch, "ID");
                int i = nextmatch.IndexOf(ID) + ID.Length;
                string udtstring = nextmatch.Substring(
                    i + 1, nextmatch.IndexOf('>') - i - 1
                    );

                var model = new UDTManage().GetTag(ID);


                if (model == null)
                {
                    continue;
                }
                if (model.Type == 0)
                {
                    if (udtstring.Length > 0)
                    {
                        TempStrBuilder.Replace(nextmatch, model.Tag.Insert(model.Tag.IndexOf('>'), udtstring));
                    }
                    else
                    {
                        TempStrBuilder.Replace(nextmatch, model.Tag);
                    }
                }
                else if (model.Type == 1)
                {
                }
                else if (model.Type == 2)
                {
                    TempStrBuilder.Replace(nextmatch,
                        string.Format("<script language=\"javascript\" type=\"text/javascript\" src=\"{0}\"></script>",
                            model.JsPath));
                }
            }
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="TempStrBuilder"></param>
        public static void Transact(StringBuilder TempStrBuilder)
        {
            IList<string> matchresult = TagEngine.TagCollection(TempStrBuilder.ToString(), "UDT");
            foreach (string nextmatch in matchresult)
            {
                TempStrBuilder.Replace(nextmatch, Transact(nextmatch));
            }
        }

        #region 执行

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="strTagString"></param>
        /// <returns></returns>
        public static string Transact(string strTagString)
        {
            StringBuilder TempStrBuilder = new StringBuilder();
            string strMode = TagEngine.GetString(strTagString, "UDT", GetWhich.ItemTemplate);
            string Sql = TagEngine.GetInnerModule(strTagString, "SqlTemplate", GetWhich.SqlTemplate);
            string URLType = TagEngine.GetValue(strTagString, "URLType");
            string ShortTitle = TagEngine.GetValue(strTagString, "ShortTitle");
            int Length = TagEngine.GetValue<int>(strTagString, "Length");

            Sql = TagEngine.ReplaceName(strTagString, Sql);

            DataSet ds = new DataSet();
            if (ds.Tables.Count == 0)
            {
                return string.Empty;
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (i >= 50)
                    {
                        break;
                    }
                    DataRow dr = ds.Tables[0].Rows[i];
                    TempStrBuilder.Append(strMode);

                    if (URLType.Contains("ShowURL"))
                    {
                        TempStrBuilder.Replace("@Link@", StaticFilePath.ContentShowUrl(dr));
                    }
                    else if (URLType == "CategoryURL")
                    {
                        TempStrBuilder.Replace("@Link@", StaticFilePath.CategoryUrl(dr));
                    }
                    else if (URLType == "SpecialURL")
                    {
                        TempStrBuilder.Replace("@Link@", StaticFilePath.SpecialUrl(dr));
                    }


                    string TempShortTitle = RepalceFiled(TempStrBuilder, ds, dr, ShortTitle);

                    TempStrBuilder.Replace("@ShortTitle@", MyString.Left(TempShortTitle, Length));
                }
            }
            else
            {
                string _Temp = TagEngine.GetInnerModule(strTagString, "UDT", GetWhich.EmptyDataTemplate);
                if (_Temp.Length == 0)
                    TempStrBuilder.Append("没有数据！");
                else
                    TempStrBuilder.Append(_Temp);
            }

            return TempStrBuilder.ToString();
        }

        #endregion

        #region

        /// <summary>
        /// 保存Js文件
        /// </summary>
        /// <param name="Path"></param>
        /// <param name="Tag"></param>
        public void SaveJs(string Path, string Tag)
        {
            string strHTML = Transact(Tag);


            new FileIO().WriterFile(HttpServer.MapPath(Path), MyJavascript.HTML2JS(strHTML));
        }

        #endregion

        /// <summary>
        /// 替换标签
        /// </summary>
        /// <param name="TempStrBuilder"></param>
        /// <param name="ds"></param>
        /// <param name="dr"></param>
        /// <param name="ShortTitleField"></param>
        /// <returns>返回将被缩短的字段的值</returns>
        private static string RepalceFiled(StringBuilder TempStrBuilder, DataSet ds, DataRow dr, string ShortTitleField)
        {
            string OutPut = "标签没有被替换";
            //支持任何标签
            foreach (DataColumn dc in ds.Tables[0].Columns)
            {
                string strTag = "@" + dc.ColumnName + "@";
                if (dc.DataType == typeof (DateTime))
                {
                    TempStrBuilder.Replace(strTag, Convert.ToDateTime(dr[dc.ColumnName]).ToString("yyyy-MM-dd"));
                }
                else
                {
                    TempStrBuilder.Replace(strTag, dr[dc.ColumnName].ToString());
                }
                if (dc.ColumnName.ToLower() == ShortTitleField.ToLower())
                {
                    OutPut = dr[dc.ColumnName].ToString();
                }
            }

            return OutPut;
        }
    }
}